package org.swqsbssddqs.shortlink.project.mq.consumer;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.swqsbssddqs.shortlink.project.config.ShortLinkGotoWhiteList;
import org.swqsbssddqs.shortlink.project.dao.entity.*;
import org.swqsbssddqs.shortlink.project.dao.mapper.*;
import org.swqsbssddqs.shortlink.project.dto.biz.ShortLinkStatsRecordDTO;
import org.swqsbssddqs.shortlink.project.mq.producer.DelayShortLinkStatsProducer;
import org.swqsbssddqs.shortlink.project.mq.producer.ShortLinkStatsSaveProducer;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;
import java.util.HashMap;

import static org.swqsbssddqs.shortlink.project.common.constant.KafkaTopicConstant.STATS_SAVE_KAFKA_TOPIC;
import static org.swqsbssddqs.shortlink.project.common.constant.RedisCacheConstant.LOCK_UPDATE_GID_KEY;

@Component
@RequiredArgsConstructor
@Slf4j
public class KafkaShortLinkStatsSaveConsumer{
//    private final RBloomFilter<String> shortLinkCreateCachePenetrationBloomFilter;
//    private final ShortLinkGotoMapper shortLinkGotoMapper;
//    private final StringRedisTemplate stringRedisTemplate;
//    private final RedissonClient redissonClient;
//    private final ShortLinkAccessStatsMapper shortLinkAccessStatsMapper;
//    private final ShortLinkLocaleStatsMapper shortLinkLocaleStatsMapper;
//    private final ShortLinkMapper shortLinkMapper;
//    private final ShortLinkOsStatsMapper shortLinkOsStatsMapper;
//    private final ShortLinkBrowserStatsMapper shortLinkBrowserStatsMapper;
//    private final ShortLinkDeviceStatsMapper shortLinkDeviceStatsMapper;
//    private final ShortLinkNetworkStatsMapper shortLinkNetworkStatsMapper;
//    private final ShortLinkAccessLogsMapper shortLinkAccessLogsMapper;
//    private final ShortLinkStatsTodayMapper linkStatsTodayMapper;
//    private final DelayShortLinkStatsProducer delayShortLinkStatsProducer;
//    private final ShortLinkGotoWhiteList shortLinkGotoWhiteList;
//    private final ShortLinkStatsSaveProducer shortLinkStatsSaveProducer;
//    @Value("${short-link.locale.amap-api-key}")
//    private String amap_api_key;
//    @Value("${short-link.locale.url}")
//    private String amap_api_url;
//
//
//    @KafkaListener(topics = STATS_SAVE_KAFKA_TOPIC, groupId = "group1")
//    public void processLoginMessage(@Payload String message, @Header("type") String type) {
//        ShortLinkStatsRecordDTO msg = JSON.parseObject(message, getType(type));
//        actualShortLinkStats(msg);
//        log.info("process login message: {}", msg);
//        KafkaProperties.Consumer c = null;
//    }
//
//    @SuppressWarnings("unchecked")
//    private static <T> Class<T> getType(String type) {
//        try {
//            return (Class<T>) Class.forName(type);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }
//    }
//    public void actualShortLinkStats(ShortLinkStatsRecordDTO param){
//        String fullShortUrl = param.getFullShortUrl();
//        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(String.format(LOCK_UPDATE_GID_KEY, fullShortUrl));
//        RLock rLock = readWriteLock.readLock();
//        if(!rLock.tryLock()){
//            log.error("获取读锁失败，加入阻塞队列");
//            delayShortLinkStatsProducer.send(param);
//            return;
//        }
//        try {
//            Date currentDate = param.getCurrentDate();
//            //统计ipV//统计uv，pv
//            String actualIp = param.getRemoteAddr();
//            ShortLinkAccessStatsDO accessStatsDO = ShortLinkAccessStatsDO.builder()
//                    .fullShortUrl(fullShortUrl)
//                    .date(currentDate)
//                    .uv(param.getUvFirstFlag()?1:0)
//                    .pv(1)
//                    .uip(param.getUipFirstFlag()?1:0)
//                    .hour(LocalTime.now().getHour())
//                    .weekday(LocalDate.now().getDayOfWeek().getValue())
//                    .build();
//            shortLinkAccessStatsMapper.shortLinkAccessStats(accessStatsDO);
//
//
//            //统计地区相关
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("key",amap_api_key);
//            map.put("ip",actualIp);
//            String s = HttpUtil.get(amap_api_url,map);
//            JSONObject localeResultObj = JSON.parseObject(s);
//            String infoCode = localeResultObj.getString("infocode");
//            String actualProvince = "未知";
//            String actualCity = "未知";
//            if (StrUtil.isNotBlank(infoCode) && StrUtil.equals(infoCode, "10000")) {
//                String province = localeResultObj.getString("province");
//                boolean unknownFlag = StrUtil.equals(province, "[]");
//                ShortLinkLocaleStatsDO linkLocaleStatsDO = ShortLinkLocaleStatsDO.builder()
//                        .province(actualProvince = unknownFlag ? actualProvince : province)
//                        .city(actualCity = unknownFlag ? actualCity : localeResultObj.getString("city"))
//                        .adcode(unknownFlag ? "未知" : localeResultObj.getString("adcode"))
//                        .cnt(1)
//                        .fullShortUrl(fullShortUrl)
//                        .country("中国")
//                        .date(currentDate)
//                        .build();
//                shortLinkLocaleStatsMapper.shortLinkLocaleState(linkLocaleStatsDO);
//            }
//
//            //统计操作系统相关
//            String os = param.getOs();
//            ShortLinkOsStatsDO linkOsStatsDO = ShortLinkOsStatsDO.builder()
//                    .os(os)
//                    .cnt(1)
//                    .fullShortUrl(fullShortUrl)
//                    .date(currentDate)
//                    .build();
//            shortLinkOsStatsMapper.shortLinkOsState(linkOsStatsDO);
//
//            //统计浏览器相关
//            String browser = param.getBrowser();
//            ShortLinkBrowserStatsDO browserStatsDO = ShortLinkBrowserStatsDO.builder()
//                    .browser(browser)
//                    .cnt(1)
//                    .fullShortUrl(fullShortUrl)
//                    .date(currentDate)
//                    .build();
//            shortLinkBrowserStatsMapper.shortLinkBrowserState(browserStatsDO);
//
//            //统计设备
//            String device = param.getDevice();
//            ShortLinkDeviceStatsDO deviceStatsDO = ShortLinkDeviceStatsDO.builder()
//                    .device(device)
//                    .cnt(1)
//                    .fullShortUrl(fullShortUrl)
//                    .date(currentDate)
//                    .build();
//            shortLinkDeviceStatsMapper.shortLinkDeviceState(deviceStatsDO);
//
//            //统计网络
//            String network = param.getNetwork();
//            ShortLinkNetworkStatsDO networkStatsDO = ShortLinkNetworkStatsDO.builder()
//                    .network(network)
//                    .cnt(1)
//                    .fullShortUrl(fullShortUrl)
//                    .date(currentDate)
//                    .build();
//            shortLinkNetworkStatsMapper.shortLinkNetworkState(networkStatsDO);
//
//            //为实现高频IP访问，记录访问日志
//            ShortLinkAccessLogsDO accessLogsDO = ShortLinkAccessLogsDO.builder()
//                    .browser(browser)
//                    .user(param.getUv())
//                    .network(network)
//                    .locale(StrUtil.join("-","中国",actualProvince,actualCity))
//                    .device(device)
//                    .fullShortUrl(fullShortUrl)
//                    .os(os)
//                    .locale(StrUtil.join("-","中国",actualProvince,actualCity))
//                    .ip(actualIp)
//                    .build();
//            shortLinkAccessLogsMapper.insert(accessLogsDO);
//            LambdaQueryWrapper<ShortLinkGotoDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkGotoDO.class)
//                    .eq(ShortLinkGotoDO::getFullShortUrl, fullShortUrl);
//            ShortLinkGotoDO shortLinkGotoDO = shortLinkGotoMapper.selectOne(queryWrapper);
//            String gid = shortLinkGotoDO.getGid();
//            shortLinkMapper.uVpVipVIncrement(gid,fullShortUrl,1,param.getUvFirstFlag()?1:0,param.getUipFirstFlag()?1:0);
//            ShortLinkStatsTodayDO linkStatsTodayDO = ShortLinkStatsTodayDO.builder()
//                    .todayPv(1)
//                    .todayUip(param.getUvFirstFlag()? 1 : 0)
//                    .todayUv(param.getUipFirstFlag() ? 1 : 0)
//                    .fullShortUrl(fullShortUrl)
//                    .date(currentDate)
//                    .build();
//            linkStatsTodayMapper.shortLinkTodayState(linkStatsTodayDO);
//        } finally {
//            rLock.unlock();
//        }
//    }
}
